/**
 * 作者：Administrator
 * 创建时间：2012-4-28 下午2:14:18
 */
package dragon.modules.system.menu.action;

import java.util.List;

import dragon.core.db.DBUtil;
import dragon.core.db.DML;
import dragon.core.exception.ExceptionHandler;
import dragon.core.page.Page;
import dragon.core.system.dao.OrgDao;
import dragon.core.system.dao.UserGroupDao;
import dragon.core.system.po.SysMenu;
import dragon.core.system.po.SysOrg;
import dragon.core.system.po.SysRoleMenu;
import dragon.core.system.po.SysUser;
import dragon.core.system.po.SysUserGroup;
import dragon.core.util.Handler;
import dragon.core.util.StringUtils;
import dragon.modules.system.menu.dao.MenuDaoImpl;
import dragon.modules.system.menu.dao.RoleMenuDaoImpl;
import dragon.modules.system.org.dao.OrgDaoImpl;
import dragon.modules.system.user.dao.UserDaoImpl;
import dragon.modules.system.user.dao.UserGroupDaoImpl;

/**
 * 实现功能：
 */
public class RoleMenuHandler extends Handler {
	private RoleMenuDaoImpl rmdao = new RoleMenuDaoImpl();

	public void save() throws ExceptionHandler {
		String menu = getServerInterface().getParameter("menu");
		String userid = getServerInterface().getParameter("userid");
		String orgid = getServerInterface().getParameter("orgid");
		String roleid = getServerInterface().getParameter("roleid");
		String outString = "f";
		if (!StringUtils.isEmpty(userid)) {
			String[] users = userid.split(",");
			rmdao.DeleteRoleMenu(Integer.parseInt(menu), "U", userid);
			for (int i = 0; i < users.length; i++) {
				SysRoleMenu rolemenu = new SysRoleMenu();
				rolemenu.setMenu(Integer.parseInt(menu));
				rolemenu.setKind("U");
				rolemenu.setTid(Integer.parseInt(users[i]));
				if (!rmdao.IsExist(rolemenu)) {
					rmdao.SaveRoleMenu(rolemenu);
				}

			}
		} else {
			rmdao.DeleteRoleMenu(Integer.parseInt(menu), "U", "");
		}
		if (!StringUtils.isEmpty(orgid)) {
			String[] orgs = orgid.split(",");
			rmdao.DeleteRoleMenu(Integer.parseInt(menu), "O", orgid);
			for (int i = 0; i < orgs.length; i++) {
				SysRoleMenu rolemenu = new SysRoleMenu();
				rolemenu.setMenu(Integer.parseInt(menu));
				rolemenu.setKind("O");
				rolemenu.setTid(Integer.parseInt(orgs[i]));
				if (!rmdao.IsExist(rolemenu)) {
					rmdao.SaveRoleMenu(rolemenu);
				}

			}
		} else {
			rmdao.DeleteRoleMenu(Integer.parseInt(menu), "O", "");
		}
		if (!StringUtils.isEmpty(roleid)) {
			String[] roles = roleid.split(",");
			rmdao.DeleteRoleMenu(Integer.parseInt(menu), "R", roleid);
			for (int i = 0; i < roles.length; i++) {
				SysRoleMenu rolemenu = new SysRoleMenu();
				rolemenu.setMenu(Integer.parseInt(menu));
				rolemenu.setKind("R");
				rolemenu.setTid(Integer.parseInt(roles[i]));
				if (!rmdao.IsExist(rolemenu)) {
					rmdao.SaveRoleMenu(rolemenu);
				}
			}
		} else {
			rmdao.DeleteRoleMenu(Integer.parseInt(menu), "R", "");
		}
		outString = "t";
		output(outString);
	}

	public void authorizeList() throws ExceptionHandler {
		// TODO:目前查询sql具有局限性，仅支持3级菜单授权，需要改造
		String itemsPerPage = getServerInterface().getParameter("rows");// 每页显示数据条数
		String targetPage = getServerInterface().getParameter("page");// 目标页码
		String menu = getServerInterface().getParameter("menu");
		String kind = (!StringUtils.isEmpty(getServerInterface().getParameter("kind")) ? getServerInterface().getParameter("kind").trim() : "");
		String filter = "";
		if (!kind.equals("")) {
			filter = " and RM.kind='" + kind + "' ";
		}
		SysMenu sysmenu1 = (new MenuDaoImpl()).get(DBUtil.getDatabase(), Integer.parseInt(menu));
		String psql = "";
		String ppsql = "";
		if (sysmenu1.getPid() != 0) {
			psql = "union (select RM.*,RM.kind kindname,M.name menuname ,'Inherit' permisskind,'继承授权' permisskindname from (" + "(Select RM1.*,U.username tidname from (select * from "
					+ SysRoleMenu.tableName + " where Kind='U') RM1 left join " + SysUser.tableName + " U on RM1.tid=U.id) union " + " (Select RM2.*,O.name tidname from (select * from "
					+ SysRoleMenu.tableName + "  where Kind='O') RM2 left join " + SysOrg.tableName + "  O on RM2.tid=O.id) union " + " (Select RM3.*,R.name tidname from (select * from "
					+ SysRoleMenu.tableName + "  where Kind='R') RM3 left join " + SysUserGroup.tableName + " R on RM3.tid=R.id)) RM " + " left join " + SysMenu.tableName
					+ " M on RM.menu=M.id where RM.menu=" + sysmenu1.getPid() + filter + " order by kind)";

			/* 以下代码原先在if语句的外面，发现其在外面市会报错(sysmenu1.getPid()==0)且(sysmenu1.getPid()!=0)时放到if外面和if里面等效，因此将其移到里面。 */
			SysMenu sysmenu2 = (new MenuDaoImpl()).get(DBUtil.getDatabase(), sysmenu1.getPid());
			if (sysmenu2.getPid() != 0) {
				ppsql = "union (select RM.*,RM.kind kindname,M.name menuname ,'Inherit' permisskind,'继承授权' permisskindname from (" + "(Select RM1.*,U.username tidname from (select * from "
						+ SysRoleMenu.tableName + " where Kind='U') RM1 left join " + SysUser.tableName + " U on RM1.tid=U.id) union " + " (Select RM2.*,O.name tidname from (select * from "
						+ SysRoleMenu.tableName + "  where Kind='O') RM2 left join " + SysOrg.tableName + "  O on RM2.tid=O.id) union " + " (Select RM3.*,R.name tidname from (select * from "
						+ SysRoleMenu.tableName + "  where Kind='R') RM3 left join " + SysUserGroup.tableName + " R on RM3.tid=R.id)) RM " + " left join " + SysMenu.tableName
						+ " M on RM.menu=M.id where RM.menu=" + sysmenu2.getPid() + filter + " order by kind)";
			}
		}

		String sql = "";
		sql = "(select RM.*,RM.kind kindname,M.name menuname ,'Direct' permisskind,'直接授权' permisskindname from (" + "(Select RM1.*,U.username tidname from (select * from " + SysRoleMenu.tableName
				+ " where Kind='U') RM1 left join " + SysUser.tableName + " U on RM1.tid=U.id) union " + " (Select RM2.*,O.name tidname from (select * from " + SysRoleMenu.tableName
				+ "  where Kind='O') RM2 left join " + SysOrg.tableName + "  O on RM2.tid=O.id) union " + " (Select RM3.*,R.name tidname from (select * from " + SysRoleMenu.tableName
				+ "  where Kind='R') RM3 left join " + SysUserGroup.tableName + " R on RM3.tid=R.id)) RM " + " left join " + SysMenu.tableName + " M on RM.menu=M.id where RM.menu=" + menu + filter
				+ " order by kind)";

		if (!psql.equals("")) {
			sql = sql + psql;
		}
		if (!ppsql.equals("")) {
			sql = sql + ppsql;
		}
		Page page = new Page();
		page.setItemsPerPage(itemsPerPage);
		page.setTargetPage(targetPage);
		page.setSql(sql);
		DML.query(page);
		output(page.getJsonMapResult());

	}

	public void del() throws ExceptionHandler {
		String menus = getServerInterface().getParameter("menus");
		String kinds = getServerInterface().getParameter("kinds");
		String tids = getServerInterface().getParameter("tids");
		String[] menu = menus.split(",");
		String[] kind = kinds.split(",");
		String[] tid = tids.split(",");
		String outString = "f";
		for (int i = 0; i < menu.length; i++) {
			rmdao.DeleteOneRoleMenu(Integer.parseInt(menu[i]), kind[i], tid[i]);
		}
		outString = "t";
		output(outString);
	}

	public void getValuesAndText() throws ExceptionHandler {
		String userid = "";
		String username = "";
		String orgid = "";
		String orgname = "";
		String roleid = "";
		String rolename = "";
		RoleMenuDaoImpl rmdao = new RoleMenuDaoImpl();
		String menu = getServerInterface().getParameter("menu");
		List<SysRoleMenu> rmList = rmdao.get(Integer.parseInt(menu));
		if (rmList != null) {
			if (rmList.size() > 0) {
				for (SysRoleMenu l : rmList) {
					if (l.getKind().equals("U")) {
						userid += l.getTid() + ",";
					} else if (l.getKind().equals("O")) {
						orgid += l.getTid() + ",";
					} else if (l.getKind().equals("R")) {
						roleid += l.getTid() + ",";
					}
				}

				if (userid.length() > 0) {
					userid = userid.substring(0, userid.length() - 1);
					UserDaoImpl userdao = new UserDaoImpl();
					List<SysUser> users = userdao.getUsersByIds(userid);
					if (users != null && users.size() > 0) {
						for (SysUser u : users) {
							username += u.getUser_name() + ",";
						}
						username = username.substring(0, username.length() - 1);
					}
				}

				if (orgid.length() > 0) {
					orgid = orgid.substring(0, orgid.length() - 1);
					OrgDao orgdao = new OrgDaoImpl();
					List<SysOrg> orgs = orgdao.getOrgsByIds(orgid, false);
					if (orgs != null && orgs.size() > 0) {
						for (SysOrg o : orgs) {
							orgname += o.getName() + ",";
						}
						orgname = orgname.substring(0, orgname.length() - 1);
					}
				}
				if (roleid.length() > 0) {
					roleid = roleid.substring(0, roleid.length() - 1);
					UserGroupDao usergroupdao = new UserGroupDaoImpl();
					List<SysUserGroup> usergroups = usergroupdao.getUserGroupsByIds(roleid);
					if (usergroups != null && usergroups.size() > 0) {
						for (SysUserGroup u : usergroups) {
							rolename += u.getName() + ",";
						}
						rolename = rolename.substring(0, rolename.length() - 1);
					}
				}

			}
		}
		output(userid + ";" + username + "|" + orgid + ";" + orgname + "|" + roleid + ";" + rolename);
	}

}
